#include <iostream>
#include <algorithm>
using namespace std;
#define ll long long
const ll N = 1e6 + 10;
ll a[N];

int main()
{
    ll n, k;
    cin >> n >> k;
    ll sum = 0;
    ll mi = 2e10;
    for (int i = 1; i <= n; i++)
    {
        cin >> a[i];
        sum += a[i];
        mi = min(a[i], mi);
    }
    ll add_side = 0;
    for (int i = 1; i <= k; i++)
    {
        ll cnt = 0;
        ll mi_side = 2e10;
        for (int j = i; j <= n; j += k + 1)
        {
            mi_side = min(a[j], mi_side);
            cnt++;
        }
        add_side = max(add_side, cnt * (mi_side - mi));
    }
    cout << sum - n * mi - add_side << '\n';
    return 0;
}
